package com.freestyle.common.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.freestyle.common.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Dubbo 3只接受2目的泛型，别提多恶心了
 * Created by rocklee on 2022/2/5 14:33
 */
@Deprecated
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseEntity<V,T> extends LinkedHashMap<String,V>  implements Serializable {
  private long id;
  private int errCode;
  private String message;
  private T result;
  public static <V,T> ResponseEntity<V,T> fromResult(long id, T val){
    ResponseEntity<V,T> ret = new ResponseEntity<>();
    ret.result = val;
    ret.id=id;
    return ret;
  }
  public static <V,T>ResponseEntity<V,T> fromErr(long id, int errCode, String message){
    ResponseEntity<V,T> ret=new ResponseEntity<>(id,errCode,message,(T)null);
    return ret;
  }
  @JsonIgnore
  public Map toMap(){
    Map map= JsonUtils.fromJson(JsonUtils.toJsonString(this), HashMap.class);
    return map;
  }
  public ResponseEntity(Exception pvException, Exception pvException2){
    this(pvException==null?pvException2:pvException);
  }
  public ResponseEntity( Exception pvException){
    errCode=-1;
    result=null;
    message=pvException.getMessage();
  }
}
